<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\Admin;
use plugin\admin\app\service\v1\LineService;
use plugin\admin\app\service\v1\OptionService;
use support\Request;
use support\Response;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 节点购买
 */
class V1NodeController extends Crud
{
    /**
     * 无需登录及鉴权的方法
     * @var array
     */
    protected $noNeedLogin = [
        'buynodes', 'buynodes_games',
        'orderlist',
        'orderdetail','orderdetail_game',
        'orderdetailexpire',
        'exchangelist',
        'refundlist',
        'import',
        'apiextract'
    ];

    /**
     * 需要登录无需鉴权的方法
     * @var array
     */
    protected $noNeedAuth = [
//        'buynodes', 'buynodes_games',
//        'orderlist',
//        'orderdetail',
//        'orderdetailexpire',
//        'exchangelist',
//        'refundlist',
//        'import',
//        'apiextract',
//        'iptrace',
    ];

    /**
     * @var Admin
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new Admin;
    }

    /**
     * 购买节点
     * @return Response
     * @throws Throwable
     */
    public function buynodes(Request $request): Response
    {
        $line_id = $request->input('line_id');
        return raw_view('v1-node/buynodes', ['line_id' => $line_id]);
    }

    /**
     * 购买节点-获取游戏
     * @return Response
     * @throws Throwable
     */
    public function buynodes_games(Request $request): Response
    {
        $line_id = $request->input('line_id');
        return raw_view('v1-node/buynodes_games', ['line_id' => $line_id]);
    }

    /**
     * 所有订单
     * @return Response
     * @throws Throwable
     */
    public function orderlist(Request $request): Response
    {
        $line_id = $request->input('line_id');
        return raw_view('v1-node/orderlist', ['line_id' => $line_id]);
    }

    /**
     * 所有节点
     * @return Response
     * @throws Throwable
     */
    public function orderdetail(Request $request): Response
    {
        $line_id = $request->input('line_id');
        $order_id = $request->input('order_id');
        $config = OptionService::getConfigOrder();
        return raw_view('v1-node/orderdetail', ['line_id' => $line_id, 'order_id' => $order_id, 'config' => $config]);
    }

    /**
     * 所有节点-游戏调换
     * @return Response
     * @throws Throwable
     */
    public function orderdetail_game(Request $request): Response
    {
        $line_id = $request->input('line_id');
        $ids = $request->input('ids');
        $user_id = $request->input('user_id');
        return raw_view('v1-node/orderdetail_game', ['line_id' => $line_id, 'ids' => $ids,'user_id'=>$user_id]);
    }

    /**
     * 节点过期
     * @return Response
     * @throws Throwable
     */
    public function orderdetailexpire(Request $request): Response
    {
        $line_id = $request->input('line_id');
        $order_id = $request->input('order_id');
        return raw_view('v1-node/orderdetailexpire', ['line_id' => $line_id, 'order_id' => $order_id]);
    }

    /**
     * 节点调换记录
     * @return Response
     * @throws Throwable
     */
    public function exchangelist(Request $request): Response
    {
        $line_id = $request->input('line_id');
        return raw_view('v1-node/exchangelist', ['line_id' => $line_id]);
    }

    /**
     * 节点退款
     * @return Response
     * @throws Throwable
     */
    public function refundlist(Request $request): Response
    {
        $line_id = $request->input('line_id');
        return raw_view('v1-node/refundlist', ['line_id' => $line_id]);
    }

    /**
     * 导入（续费/修改账密）
     * @return Response
     * @throws Throwable
     */
    public function import(Request $request): Response
    {
        $line_id = $request->input('line_id');
        $user_id = $request->input('user_id');
        $admin = admin();
        $config = OptionService::getConfigOrder();
        $line_list = "[]";
        if ($user_id > 0) {
            list($line_list, $line_column) = LineService::select(['id', 'name']);
            $line_id = $line_list[0]['id'];
            $line_list = json_encode($line_list,JSON_UNESCAPED_UNICODE);
        }
        return raw_view('v1-node/import', compact('user_id', 'admin', 'config', 'line_list', 'line_id'));
    }

    /**
     * API提取
     * @return Response
     * @throws Throwable
     */
    public function apiextract(Request $request): Response
    {
        return raw_view('v1-node/apiextract');
    }

    /**
     * IP黑名单
     * @return Response
     * @throws Throwable
     */
    public function iptrace(Request $request): Response
    {
        $line_id = $request->input('line_id');
        return raw_view('v1-node/iptrace', ['line_id' => $line_id]);
    }
}
